/**
 * Created by wangshaohua on 2018/1/25.
 */
moduleContent.controller("ExerciseContentMove", function ($$CambioConfig, ExerciseContentResource, $scope, $log, $state, $stateParams, GeneralResource, LoadingService, ConstantService, DialogService) {
    initPage($scope);
    $scope.$on("ngDialog.opened", function () {
        dialogOnLoad();
        $scope.queryFolder(1);
    });

    // ------------------------------------------------------------------------------------------------------
    $scope.cancel = function () {
        DialogService.close();
    };

    $scope.queryFolder = function (pageNo, direction) {
        $scope.queryFolderPara = {
            key: null,
            space: $scope.currentSpace,
            recordType: $scope.currentRecordType
        };
        if (!direction) {
            direction = 'DOWN';
        }
        $scope.queryFolderPara.parentId = $scope.currentParentId;
        ExerciseContentResource.query({pageNo: pageNo - 1, pageSize: 5, direction: direction}, $scope.queryFolderPara, function (queryResult) {
            $log.debug("查询结果", queryResult.data);
            $scope.queryFolderResult = queryResult.data.list;
            $scope.currentParentId = queryResult.data.parentId;
            $scope.pagination.setPage(queryResult.data.list, pageNo);
        });
    }

    $scope.pagination = new Pagination();
    $scope.pagination.setPageClickFn($scope.queryFolder);

    $scope.openFolder = function (record) {
        $scope.currentParentId = record.id;
        $scope.queryFolder(1);
    }

    $scope.backSpace = function () {
        $scope.queryFolder(1, 'UP');
    }

    $scope.selectFolder = function (record) {
        $scope.targetContentId = record.id;
        for (var i = 0; i < $scope.queryFolderResult.datas.length; i++) {
            if ($scope.queryFolderResult.datas[i].id != record.id) {
                $scope.queryFolderResult.datas[i].checked = false;
            }
        }
    }

    $scope.submit = function () {
        Alert.confirm(function () {
            ExerciseContentResource.move({targetContentId: $scope.targetContentId}, $scope.selectedContentIds, function (result) {
                // $scope.$parent.parentId = result.data.value;
                Alert.success();
                DialogService.close();
            });
        })
    }
    // ------------------------------------------------------------------------------------------------------
    // ------------------------------------------------------------------------------------------------------
});